﻿using System;
using System.Data;
using VitCode.Castle.DataAccess;
using System.Text;
using VitCode.Castle.Management;

namespace CRS.Web.UserControls
{
    /// <summary>
    ///		MenuCtl 的摘要说明。
    /// </summary>
    public class MenuCtl : System.Web.UI.UserControl
    {
        StringBuilder m_strBuilder = new StringBuilder();
        BaseDataAccess m_Acc = new BaseDataAccess();
        PermissionManager m_PermissionManger = new PermissionManager();
        private void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var userId =GenericContext.CurrUserId;
                if(userId != Guid.Empty)
                {
                        try
                        {
                            m_strBuilder.Append("<html xmlns=\"http://www.w3.org/1999/xhtml\" >");
                            m_strBuilder.Append("<head id=\"Head1\" runat=\"server\">");
                            m_strBuilder.Append("<title>NSCS</title>");
                            m_strBuilder.Append("</head>");
                            m_strBuilder.Append("<body>");
                            var sql =@"select distinct m.Id,m.Name,m.UrlPath,m.ParentId,m.SortKey from VitCode_Castle_Menu m "+
" inner join VitCode_Castle_Permission p on p.Resourceid=m.Id "+
" inner join VitCode_Castle_role r on r.Id=p.ObjId "+
" inner join VitCode_Castle_UserRoleRelation ur on ur.RoleId=r.Id  "+
" and ur.UserId='"+userId+@"' "+
" order by m.SortKey";
                            DataTable dt = m_Acc.GetDataTableBySql(sql);
                            if (dt == null || dt.Rows.Count == 00)
                                return;
                            var rsParentMenu = dt.Select("ParentId is null");
                            if (rsParentMenu.Length > 0)
                            {
                                m_strBuilder.Append("<div id=\"submenucontent\" style=\"margin-top:0px; margin-left:0px;\">");
                                m_strBuilder.Append(
                                    "<div class=\"submenu\" style=\"margin-top:0px; margin-left:0px; border:1px;font-size:12px;\">");
                                
                                for (int i = 0; i < rsParentMenu.Length; i++)
                                {
                                    var strPName = Convert.ToString(rsParentMenu[i]["Name"]);
                                    var strPId= Convert.ToString(rsParentMenu[i]["Id"]);

                                    m_strBuilder.Append("<p onclick=\"changeView(this.id)\" onmouseover=\"this.style.backgroundImage='url(../Images/opinion_h2.png)';\" onmouseout=\"this.style.backgroundImage='url(../Images/submenu_h2.gif)';\" id=\"submenuh2_" + i + "\"><img src=\"../Images/ico_nohave.gif\" id=\"ico_submenuh2_" + i + "\" alt=\"\" />" + strPName + "</p> ");
                                    m_strBuilder.Append("<div class=\"navigation\" id=\"div_submenuh2_" + i + "\" style=\"display:none\">");
                                    m_strBuilder.Append("<table>");

                                    var rsChildMenu = dt.Select("ParentId='" + strPId + "'");
                                    if (rsChildMenu.Length > 0)
                                    {
                                        for (int j = 0; j < rsChildMenu.Length; j++)
                                        {
                                            string strCId = rsChildMenu[j]["Id"].ToString();
                                            string strCName = rsChildMenu[j]["Name"].ToString();
                                            string strCUrl = rsChildMenu[j]["UrlPath"].ToString();
   
                                            m_strBuilder.Append("<tr>");
                                            m_strBuilder.Append("<td>&nbsp;&nbsp;<img src=\"../Images/arr.gif\"/></td>");
                                            m_strBuilder.Append("<td onmouseover=\"this.style.backgroundColor='#BEE4F7';\" onmouseout=\"this.style.backgroundColor='#FFFFFF';\"><a href=\"");
                                            m_strBuilder.Append(strCUrl);
                                            m_strBuilder.Append(strCUrl.Length > 0 && strCUrl.IndexOf("?") > 0 ? "&" : "?");
                                            string urlPath = strCUrl;
                                            if (urlPath == "../BackStageManagement/Main.aspx")
                                            {
                                                if (GenericContext.CheckCurrUserIsAdmin())
                                                {
                                                    m_strBuilder.Append("MenuId=" + strCId +
                                                                        "\" expanded=\"false\"   nodedata=\"2\" target=\"_Blank\" id=\"" +
                                                                        strCId + "\"><div style=\"width: 150px; cursor:hand;\" id=\"" + i + "." + j + "\" onclick=\"lightHide(this)\">&nbsp;&nbsp;" + strCName);
                                                }
                                            }
                                            else
                                            {
                                                m_strBuilder.Append("MenuId=" + strCId+ "\" expanded=\"false\"   nodedata=\"2\" target=\"frmmain\" id=\"" +
                                                             strCId+ "\"><div style=\"width: 150px; cursor:hand;\" id=\"" + i + "." + j + "\" onclick=\"lightHide(this)\">&nbsp;&nbsp;" + strCName);
                                            }
                                            m_strBuilder.Append("</div></a></td></tr>");
                                        }

                                    }
                                    m_strBuilder.Append("</table>");
                                    m_strBuilder.Append("</div>");

                                }
                                m_strBuilder.Append("</div>");
                                m_strBuilder.Append("</div>");



                            }
                            m_strBuilder.Append("</body>");
                            m_strBuilder.Append("</html>");
                            Response.Write(m_strBuilder);
                        }
                        finally
                        {
                            m_Acc.CloseSession();
                        }
                    }
                
            }

        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		设计器支持所需的方法 - 不要使用代码编辑器
        ///		修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion
    }
}
